<?php
$auth = Auth::getAuth('current_user');
$db = new Database;
$fs = new functions;

if(isset($_POST['action'])){
	$date = $fs->currentDateTime();
	
	switch($_POST['action']){
		
		case "saveOrgchart":
			$orgchart = $_POST['json_encode'];
			$json = json_decode($orgchart, true);
			//DEACTIVATE OLD ORGCHART
			if($json['status']==1){
				$update_active = array('status' =>  '0');
				$where_active = array('company_id' =>  $auth['company_id']);
				$db->update("tborgchart",$update_active,$where_active);
			}
			//INSERT ORGCHART DETAILS
			$insert = array("company_id"	=>	$auth['company_id'],
					"title"		=>	$json['title'],
					"description"	=>	$json['description'],
					"date"		=>	$date,
					"status"	=>	$json['status'],
					"json"	=>	json_encode(array("width"=>$json['width'],"height"=>$json['height'])),
					"is_active"	=>	1
					);
			$orgchart_id = $db->insert("tborgchart",$insert);
			//INSERT ORGCHART OBJECTS
			foreach($json['nodes'] as $nodes){
				$insert_obects = array("orgChart_id"	=>	$orgchart_id,
						       "object_id"	=>	$nodes['node_data_id'],
						       "department" =>	$nodes['node_text'],
						       "json" 		=>	json_encode($nodes),
							);
				$department_id = $db->insert("tborgchartobjects",$insert_obects);
				if($json['status']==1){
					//head
					$head = array('id'=>$nodes['orgchart_user_head'][0]);
					$head_set = array("department_id"=>$department_id,
											"department_position_level"=>1);
					$db->update("tbuser",$head_set,$head);
					//assistant head
					$asst_head = array('id' => $nodes['orgchart_dept_assistant'][0]);
					$asst_head_set = array("department_id"=>$department_id,
											"department_position_level"=>2);
					$db->update("tbuser",$asst_head_set,$asst_head);
					//members
					foreach ($nodes['orgchart_dept_members'] as $user_id) {
						$members_array = array("id"=>$user_id);
						//echo "members".$user_id;
						$members_set = array("department_id"=>$department_id,
											"department_position_level"=>3);
						$db->update("tbuser",$members_set,$members_array);
					};
					// //form update
					// //  Authors
					// //  Viewers
					// //  Form Users
					// $department = $nodes['node_text'];

					// $getForm = $db->query("SELECT * FROM tb_workspace WHERE company_id = '". $auth['company_id'] ."'","array");
					// foreach ($getForm as $key => $value) {
					// 	//SEARCH JSON
					// 	$form_authors = $value['form_authors'];
					// 	$form_viewers = $value['form_viewers'];
					// 	$form_json = $value['form_json'];

					// 	//PARSE JSON
					// 	$form_authors = json_decode($form_authors);
					// 	$form_viewers = json_decode($form_viewers);
					// 	$form_json = json_decode($form_json,true);
					// 	$form_users = json_decode($form_json['form_users']);

					// 	//inner json
					// 	$form_json['form_json']['form-authors'] = $form_authors;
					// 	$form_json['form_json']['form-readers'] = $form_viewers;
					// 	$form_json['form_json']['form-users'] = $form_users;
					// 	//PROCESS new JSON
					// 	$form_authors =  $fs->changeDepartments($form_authors, $department, $department_id);
					// 	$form_viewers =  $fs->changeDepartments($form_viewers, $department, $department_id);
					// 	$form_users = $fs->changeDepartments($form_users, $department, $department_id);

					// 	$form_json['form_authors'] = $form_authors;
					// 	$form_json['form_viewers'] = $form_viewers;
					// 	$form_json['form_users'] = $form_users;

					// 	//encode json
					// 	$form_json = json_encode($form_json,JSON_UNESCAPED_SLASHES);

					// 	//UPDATE HERE

					// 	$form_id = array("id"=>$value['id']);
					// 	$form_update = array("form_authors"=>$form_authors,
					// 						"form_viewers"=>$form_viewers,
					// 						"form_json"=>$form_json);
					// 	$db->update("tb_workspace",$form_update,$form_id);
					// }
					// $getPostQuery = "SELECT p.postPrivacyIDs, p.id FROM tbpost p LEFT JOIN tbuser u ON p.postedBy = u.id WHERE u.company_id =  '". $auth['company_id'] ."' AND p.postPrivacyType =  '2'";
					// $getPost = $db->query($getPostQuery,"array");
					// foreach ($getPost as $key => $value) {
					// 	$postPrivacyIDs = $fs->changeDepartmentsArray($value['postPrivacyIDs'], $department, $department_id);

					// 	//Update Here
					// 	$post_id = array("id"=>$value['id']);
					// 	$post_update = array("postPrivacyIDs"=>$postPrivacyIDs);
					// 	$db->update("tbpost",$post_update,$post_id);
					// 	echo "ok";
					// }

					// //END
				}
			}
			
			//INSERT ORGCHART LINES
			foreach($json['lines'] as $lines){
				$insert_obects = array("orgChart_id"	=>	$orgchart_id,
						       "parent"		=>	$lines['parent'],
						       "child" 		=>	$lines['child'],
						       "json" 		=>	json_encode($lines),
							);
				$db->insert("tborgchartline",$insert_obects);
			}
			
				echo "Your Organizatinal Chart (" . $json['title'] . ") was successfully saved.";
		break;
		case "getHeadName":
			$id = $_POST['userID'];
			$person = new Person($db,$id);
			echo $person->first_name." ".$person->last_name;
		break;
	}
	
}

?>
